#!/bin/bash
set -e


function help()
{
    echo -e "usage:"
    echo -e "edit /mnt/etc/fstab first with blkid info"
    echo -e "./ubuntu_restore_efi.sh --efi_disk /dev/nvme0n1 --efi_part /dev/nvme0n1p1"
    echo -e "./ubuntu_restore_efi.sh -d /dev/sdb -p /dev/nvme0n1p1"

}


while [ -n "$1" ]
do
    case "$1" in
        --efi_disk|-d) 
            EFI_DISK=$2
            shift
            ;;
        --efi_part|-p) 
            EFI_PART=$2
            shift
            ;;
        --help|-h) 
            help
            exit 1
            ;;
        *) echo "[-] Parameters of illegal !!!"
            help
            exit 1
            ;;
    esac
    shift
done


echo "EFI_DISK=${EFI_DISK}"
echo "EFI_PART=${EFI_PART}"



if [[ -z ${EFI_DISK}  || -z ${EFI_PART} ]]
then
    echo -e "\e[31mParameters invalid !!!\e[0m"
    exit 1
fi
echo -e "\e[36mParameters OK !!!\e[0m"

set +e
cd /mnt
sudo mkdir dev proc sys run mnt media lost+found
set -e

# 如果只有ubuntu的话，都不用挂boot
sudo mount ${EFI_PART} /mnt/boot/efi

for i in /dev /proc /sys /run /dev/pts; do sudo mount -B $i /mnt$i; done

echo "next please enter:"
echo "grub-install ${EFI_DISK}"
echo "update-grub"

sudo chroot /mnt








